package com.kayt.cyberBar.cpublic.business.dao.impl;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cpublic.business.dao.IPublicReqTypeDao;
import com.kayt.cyberBar.cpublic.business.entity.po.CPublicReqType;

@Repository("publicReqTypeDao")
public class PublicReqTypeDaoImpl implements IPublicReqTypeDao {
	@Autowired
	private EntityDao entityDao;
	
	@Override
	public Boolean checkExist(String name) throws SQLException {
		String hql = "select count(o.id) as count from CPublicReqType o where o.reqTypeName=?";
        List tempMapList;
        Boolean exist = false;
        tempMapList = this.entityDao.getPagingDataByHql(-1, -1, hql, new Object[]{name});
        if(tempMapList != null && !tempMapList.isEmpty()) {
            Map tempMap = (Map)tempMapList.get(0);
            Long tempCount = (Long)tempMap.get("count");
            if (tempCount > 0) {
            	exist = true;
            }
        }
		return exist;
	}
	@Override
	public CPublicReqType findById(String reqTypeId) {
		if(StringUtils.isNotEmpty(reqTypeId)){
			Long id = null;
			try{
				id = Long.parseLong(reqTypeId);
			}catch (Exception e) {
				return null;
			}
			return entityDao.findById(id, CPublicReqType.class);
		}else{
			return null;
		}
	}

	@Override
	public Integer delete(String reqTypeId) {
		if(StringUtils.isNotEmpty(reqTypeId)){
			Long id = null;
			try{
				id = Long.parseLong(reqTypeId);
				CPublicReqType CPublicReqType = entityDao.findById(id, CPublicReqType.class);
				if(CPublicReqType != null){
					entityDao.delete(CPublicReqType);
					return 0;
				}else{
					return -1;
				}
			}catch (Exception e) {
				return -1;
			}
		}else{
			return -1;
		}
	}

	@Override
	public void modify(CPublicReqType entity) throws SQLException {
		entityDao.update(entity);
	}

	@Override
	public PageData<CPublicReqType> findByPage(String hql, Object[] params, LinkedHashMap<String, String> orderMap) throws SQLException {
		PageData<CPublicReqType> reqTypePageData = this.entityDao.getPagingData(CPublicReqType.class, -1, -1, hql, params, orderMap);
		return reqTypePageData;
	}

	@Override
	public Long save(CPublicReqType entity) throws SQLException {
		return this.entityDao.save(entity);
	}

	@Override
	public PageData<CPublicReqType> getReqTypeList(LinkedHashMap<String, String> orderMap) throws SQLException {
		PageData<CPublicReqType> reqTypePageData = this.entityDao.getPagingData(CPublicReqType.class, -1, -1, null, null, orderMap);
		return reqTypePageData;
	}
}
